package com.kucunProject.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.hibernate.id.UUIDHexGenerator;

import com.kucunProject.dao.DBManager;
import com.kucunProject.model.BrandInfo;

public class BrandInfoService {

	public boolean addInfo(String name) {
		boolean flag = true;

		DBManager man = new DBManager();
		String sql = "insert into tb_kc_brand_info values(?,?,?)";
		PreparedStatement ps = null;
		String id = getId();

		try {
			ps = man.connect().prepareStatement(sql);
			ps.setString(1, id);
			ps.setString(2, name);
			ps.setInt(3, 0);
			flag = ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;
	}

	public ArrayList<BrandInfo> selectInfo(String name) {
		ArrayList<BrandInfo> list = new ArrayList<BrandInfo>();
		ResultSet rs = null;
		DBManager man = new DBManager();
		PreparedStatement ps = null;
		StringBuffer sql = new StringBuffer();
		sql.append("select * from tb_kc_brand_info where 1=1 ");
		if (name != null && !"".equals(name)) {
			sql.append("and brand_name like '%").append(name).append("%'");
		}
		try {
			ps = man.connect().prepareStatement(sql.toString());
			rs = ps.executeQuery();
			while (rs.next()) {
				BrandInfo bi = new BrandInfo();
				bi.setBrandName(rs.getString("BRAND_NAME"));
				bi.setBiId(rs.getString("BI_ID"));
				list.add(bi);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	public BrandInfo selectInfo2(String id) {
		BrandInfo bi = new BrandInfo();
		ResultSet rs = null;
		DBManager man = new DBManager();
		PreparedStatement ps = null;
		String sql = "select * from tb_kc_brand_info where bi_id = ?";
		try {
			ps = man.connect().prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				bi.setBrandName(rs.getString("BRAND_NAME"));
				bi.setBiId(id);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bi;
	}
	
	public boolean deleteInfo(String id) {
		boolean flag = true;
		System.out.println(id);
		DBManager dbm = new DBManager();
		StringBuffer sql = new StringBuffer();
		PreparedStatement ps = null;
		sql.append("delete from tb_kc_brand_info where 1=1");

		try {
			if (id != null && !"".equals(id)) {
				sql.append("and bi_id = '").append(id).append("'");
				ps = dbm.connect().prepareStatement(sql.toString());
				flag = ps.execute();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;
	}

	public boolean updateInfo(String id,String name) {

		boolean flag = true;
		DBManager dbm = new DBManager();
		String sql = "update tb_kc_brand_info set brand_name = ? where bi_id = ?";
		PreparedStatement ps = null;

		try {
			ps = dbm.connect().prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, id);
			flag = ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return flag;
	}

	public static String getId() {
		String id = (String) new UUIDHexGenerator().generate(null, null);
		return id;
	}

}
